// pages/goodsDetail/goodsDetail.ts
Page({

  /**
   * 页面的初始数据
   */
  data: {
    goodsId: "",
    baseImgUrl: getApp().globalData.IMG_URL,
    goodsInfo: { tenantId: "", pGgList: [] },
    storeInfo: null,
    param: "1",
    background: [""],
    goodsUrl: [""],
    buttons: [{ text: '取消' }, { text: '确认' }]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    var pageObj = this
    const eventChannel = this.getOpenerEventChannel()
    eventChannel.on('acceptDataFromOpenerPage', function (param) {
      console.error("参数：" + JSON.stringify(param))
      pageObj.setData({ goodsId: param.goodsId })
      // 获取商品信息
      pageObj.getOpenGoodsInfo(pageObj.data.goodsId)

    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  goodsDetail(param: any): void {
    wx.navigateTo({
      url: '/pages/goods/goodsDetail/goodsDetail',
      events: {
      },
      success: function (res) {
        // 通过eventChannel向被打开页面传送数据
        res.eventChannel.emit('acceptDataFromOpenerPage', { goodsId: param.currentTarget.dataset.id })
      }
    })
  },
  gotoStoreDetailPage(param: any): void {
    // console.error("gotoStoreDetailPage: "+JSON.stringify(param))
    wx.navigateTo({
      url: '/pages/storeOpenDetail/storeOpenDetail',
      events: {
      },
      success: function (res) {
        // 通过eventChannel向被打开页面传送数据
        res.eventChannel.emit('acceptDataFromOpenerPage', { storeId: param.currentTarget.dataset.id })
      }
    })
  },
  getOpenGoodsInfo(goodsId: any): void {
    var that = this
    // const token = wx.getStorageSync('token')
    const preUrl = getApp().globalData.SERVER_URL
    const url = preUrl + "fc/goods/client/anon/queryOpenInfoById"
    wx.request({
      url: url,
      method: 'GET',
      data: {
        id: goodsId
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded', // 默认值
        // 'X-Access-Token': token
      },
      success(res: any) {
        // console.error("res: " + JSON.stringify(res))
        wx.stopPullDownRefresh()
        if (res.data.code == 200 && res.data.result != null) {
          that.setData({ goodsInfo: res.data.result })
          that.getStoreOpenInfo(that.data.goodsInfo.tenantId)

          if (res.data.result.goodsCover != null) {
            var chars: string = res.data.result.goodsCover
            // console.error("chars: " + chars)
            var lastChar = chars.substring(chars.length - 1)
            // console.error("lastChar: " + lastChar)
            if ("," == lastChar) {
              chars = chars.substring(0, chars.length - 1)
            }
            // console.error("chars：" + chars)
            var charsArr: string[] = chars.split(',');
            that.setData({ background: charsArr })
            // console.error("data: " + JSON.stringify(that.data))
          }

          if (res.data.result.goodsUrl != null) {
            var goodsUrl: string = res.data.result.goodsUrl.replace(/(s*$)/g, "")
            // console.error("goodsUrl: " + goodsUrl)
            var lastChar = goodsUrl.substring(goodsUrl.length - 1)
            if ("," == lastChar) {
              goodsUrl = goodsUrl.substring(0, goodsUrl.length - 1)
            }
            // console.error("goodsUrl2: " + goodsUrl)
            const chars: string[] = goodsUrl.split(',');
            // console.error("chars: " + chars)

            that.setData({ goodsUrl: chars })
            // console.error("data: " + JSON.stringify(that.data))
          }
        } else {
          console.info(JSON.stringify(res))
        }
      },
    })
  },
  getStoreOpenInfo(storeId: string): void {
    var that = this
    // const token = wx.getStorageSync('token')
    const preUrl = getApp().globalData.SERVER_URL
    const url = preUrl + "fc/store/client/anon/queryOpenInfoById"
    wx.request({
      url: url,
      method: 'GET',
      data: {
        id: storeId,
        needGoods: true
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded', // 默认值
        // 'X-Access-Token': token
      },
      success(res: any) {
        // console.error("res:"+JSON.stringify(res))
        wx.stopPullDownRefresh()
        if (res.data.code == 200 && res.data.result != null) {
          that.setData({ storeInfo: res.data.result })
          // console.error("storeInfo: " + JSON.stringify(that.data.storeInfo))
        } else {
          console.error(JSON.stringify(res))
        }
      },
    })
  },
  doCall(param: any) {
    var phone = param.currentTarget.dataset.phone
    wx.makePhoneCall({
      phoneNumber: phone,
      success: function (res) {
        console.log('拨打电话成功！');
      },
      fail: function (res) {
        console.log('拨打电话失败！');
      }
    })
  },
  doNavi(param: any) {
    const that = this

    wx.openLocation({
      latitude: that.data.storeInfo.locLat, // 纬度，范围为-90~90，负数表示南纬
      longitude: that.data.storeInfo.locLng, // 经度，范围为-180~180，负数表示西经
      scale: 15, // 缩放比例
      name: that.data.storeInfo.storeName,
      address: that.data.storeInfo.locAddress,
      success: function (r) {
        console.log(r)
      }
    })
  },
})